package com.platform.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.platform.constant.PlatfromConstants;
import com.platform.dao.ApiAddressMapper;
import com.platform.entity.AddressVo;
import com.platform.util.HttpClientUtils;
import com.platform.util.TFAddressUtils;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


@Service
public class ApiAddressService {

  private Logger logger = LoggerFactory.getLogger(getClass());


  @Autowired
  private ApiAddressMapper addressDao;


  public AddressVo queryObject(Integer id) {
    return addressDao.queryObject(id);
  }


  public List<AddressVo> queryList(Map<String, Object> map) {
    return addressDao.queryList(map);
  }


  public int queryTotal(Map<String, Object> map) {
    return addressDao.queryTotal(map);
  }


  public void save(AddressVo address) {
    addressDao.save(address);
  }


  public void update(AddressVo address) {
    addressDao.update(address);
  }


  public void delete(Integer id) {
    addressDao.delete(id);
  }


  public void deleteBatch(Integer[] ids) {
    addressDao.deleteBatch(ids);
  }

  public void updateIsDefault(AddressVo addressVo) {
    addressDao.updateIsDefault(addressVo);
  }

  public List<AddressVo> queryaddressUserlist(Map<String, Object> param) {
    return addressDao.queryaddressUserlist(param);
  }


  public AddressVo queryDefaultAddress(Long userId) {
    return addressDao.queryDefaultAddress(userId);
  }


  public String checkDeliveryDistance(AddressVo entity) {
    String errmsg = "";
    try {
      StringBuilder addressBuilder = new StringBuilder();
      if (entity.getProvinceName() == null && entity.getCityName() == null
          &&entity.getCountyName() == null) {
        addressBuilder.append("辽宁省本溪市明山区");
      } else {
        addressBuilder.append(
            entity.getProvinceName() + entity.getCityName() + entity.getCountyName());
      }
      String address = addressBuilder.toString() + entity.getDetailInfo();

      TreeMap<String, Object> treeMap = TFAddressUtils.getParamInstance();
      treeMap.put("address", address);
      String requestUrl1 = TFAddressUtils
          .getRequestUrl(new StringBuilder(TFAddressUtils.GET_LOCATION), treeMap);
      JSONObject addressObject = HttpClientUtils.httpGet(requestUrl1);
      if (addressObject == null || !"0".equals(addressObject.getString("status"))) {
        errmsg =  "未查询到用户地址";
      }

      JSONObject detailResult = addressObject.getJSONObject("result");
      if (detailResult == null) {
        errmsg = "未查询到用户地址";
      }

      JSONObject location = detailResult.getJSONObject("location");
      Double lat = location.getDoubleValue("lat");
      Double lng = location.getDoubleValue("lng");
      TreeMap<String, Object> params = TFAddressUtils.getParamInstance();
      params.put("mode", "walking");
      params.put("from", "41.29517,123.789665");
      params.put("to", lat + "," + lng);
      String requestUrl = TFAddressUtils
          .getRequestUrl(new StringBuilder(TFAddressUtils.GET_DISTANCE), params);

      JSONObject jsonObject = HttpClientUtils.httpGet(requestUrl);
      JSONObject result = jsonObject.getJSONObject("result");
      JSONArray elements = result.getJSONArray("elements");

      if (!CollectionUtils.isEmpty(elements)) {
        JSONObject jsonObject1 = elements.getJSONObject(0);
        Integer distance = jsonObject1.getInteger("distance");
        if (distance > PlatfromConstants.DELIVERY_DISTANCE) {
          errmsg = "距离大于配送范围";
        }
      }
    } catch (Exception e) {
      logger.error("获取配送距离失败{}", e);
    }

    return errmsg;
  }
}
